//
//  MZYBrandViewController.m
//  WristWatch
//
//  Created by dllo on 15/10/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYBrandViewController.h"
#import "MZYHeader.h"
#import "MZYbrandTableViewCell.h"
#import "MZYsearchViewController.h"

#pragma mark 第四步: 签订协议
@interface MZYBrandViewController ()<UITableViewDataSource, UITableViewDelegate, MZYbrandTableViewCellDelegate, UISearchBarDelegate>

@property(nonatomic, retain)UITableView *tableView;

@property(nonatomic, retain)NSMutableArray *tabArr;

@property(nonatomic, retain)NSMutableDictionary *dataDic;
@property(nonatomic, retain)NSMutableDictionary *resultDic;
@property(nonatomic, copy) NSString *brand_id;
@property(nonatomic, retain) NSArray *stArr;
@property(nonatomic, retain)UISearchBar *mySearchBar;
@property(nonatomic, retain)MBProgressHUD *HUD;

@property(nonatomic, retain)NSMutableArray *huancunArr;

@end

@implementation MZYBrandViewController

- (void)dealloc {
    [_tableView release];
    [_tabArr release];
    [_HUD release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"品牌";
    [self createView];
    [self createData];

    
}

- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 100;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView release];
    
    
    // 建立小菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.detailsLabelText = @"请稍后";
    self.HUD.dimBackground = YES;

    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"11"] style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    

}



#pragma mark search 点击方法

- (void)click:(UIBarButtonItem *)button {
    MZYsearchViewController *searchVC = [[MZYsearchViewController alloc] init];
//    [self.navigationController presentViewController:searchVC animated:YES completion:^{
//    
//    }];
    [self.navigationController pushViewController:searchVC animated:YES];
    [searchVC release];
}

#pragma mark 数据解析
- (void)createData {
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    [manager GET:@"http://api.ttime.com/searchApi/screenPchile?token=f1cfa3eafd64a3474df1d5567d830da6&uid=0&flag=app&from=null&v=2.3.0&cityid=1912" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//    self.dataDic = responseObject;
//    self.tabArr = [NSMutableArray array];
//    self.resultDic = self.dataDic[@"result"];
//    self.stArr = self.resultDic.allKeys;
//    self.stArr = [self.stArr sortedArrayUsingSelector:@selector(compare:)];
//    for (NSString *key in self.stArr) {
//    NSArray *arr = [NSArray array];
//    arr = [MZYbrandModel MZYBaseModelByArr:[self.resultDic objectForKey:key]];
//            
//        [self.tabArr addObject:arr];
//    }
//        
//        self.HUD.hidden = YES;
//        [self.tableView reloadData];
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    }];
    
    NSURL *url = [NSURL URLWithString:@"http://api.ttime.com/searchApi/screenPchile?token=f1cfa3eafd64a3474df1d5567d830da6&uid=0&flag=app&from=null&v=2.3.0&cityid=1912"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            
            
            self.dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            
            
            
            self.tabArr = [NSMutableArray array];
                self.resultDic = self.dataDic[@"result"];
                self.stArr = self.resultDic.allKeys;
                self.stArr = [self.stArr sortedArrayUsingSelector:@selector(compare:)];
                for (NSString *key in self.stArr) {
                NSArray *arr = [NSArray array];
                arr = [MZYbrandModel MZYBaseModelByArr:[self.resultDic objectForKey:key]];
            
                    [self.tabArr addObject:arr];
                }
                    
                    self.HUD.hidden = YES;
                    [self.tableView reloadData];
        }
        
    }];
    
    NSURLCache *cache = [NSURLCache sharedURLCache];
    NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
    if (response) {
        NSLog(@"源这个请求已存在缓存");
    } else {
        NSLog(@"源这个请求没有缓存");
    }

    
    


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *lreuse = @"lreuse";
        MZYbrandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:lreuse];
        if (!cell) {
            cell = [[[MZYbrandTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:lreuse] autorelease];
        }
//    cell.arr = self.tabArr[indexPath.row];
//    cell.arr = self.tabArr;
//    cell.arr = self.tabArr[indexPath.section];
//    NSLog(@"%@", self.tabArr[indexPath.section]);
    cell.arr = self.tabArr[indexPath.section];
//    for (MZYbrandModel *model in self.tabArr[indexPath.row]) {
//        NSLog(@"%@", model.brand_name);
//    }
    
    // 代理collectionView 跳转界面
#pragma mark 第五步: 设置代理人
    cell.myDelegate = self;
    return cell;
}



#pragma mark 第六步: 实现协议方法
- (void)takeValue:(UIViewController *)MZYbrandinfoViewController {
    
    [self.navigationController pushViewController:MZYbrandinfoViewController animated:YES];
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.stArr.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    NSLog(@"%@", self.resultDic.allKeys);
//    return self.resultDic.allKeys[section];/
    return self.stArr[section];
}

// ROW Height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.tabArr[indexPath.section] count] % 2 == 0) {
        return ([self.tabArr[indexPath.section] count] / 2) * 100;
    }
    
    return ([self.tabArr[indexPath.section] count] / 2 + 1) * 100;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
